
import '../coroutine/coroutine.dart';

extension CUCUListExtension<T> on List<T> {
  T? getOrNull(int index) {
    return index >= 0 && length > index ? this[index] : null;
  }
}

extension CUListNullExtension<T> on List<T>? {
  bool get isNullOrEmpty => this == null || this!.isEmpty;
}

extension CUIterableNumExtension on Iterable<num> {
  //平均值
  double average() {
    if (isEmpty) {
      return 0.0; // 或者抛出异常：throw Exception("列表不能为空");
    }
    final sum = fold(0.0, (prev, current) => prev + current);
    return sum / length;
  }
}

extension CUIterableExtension<T> on Iterable<T> {
  T? firstWhereX(bool Function(T element) test, {T? def}) {
    try {
      return firstWhere(test);
    } catch (e) {
      return def;
    }
  }

  List<T> sortedBy<R>(R? Function(T element) selector) {
    final List<T> list = toList();
    list.sort((a, b) {
      final R? valueA = selector(a);
      final R? valueB = selector(b);

      if (valueA == null && valueB == null) return 0;
      if (valueA == null) return 1;
      if (valueB == null) return -1;
      if (valueA is Comparable<R> && valueB is Comparable<R>) {
        return valueA.compareTo(valueB);
      } else if (valueA is num && valueB is num) {
        return valueA.compareTo(valueB);
      } else if (valueA is bool && valueB is bool) {
        return valueA ? -1 : 1;
      } else {
        throw ArgumentError('Selector must return a Comparable type');
      }
    });
    return list;
  }

  List<T> sortedByDescending<R>(R? Function(T element) selector) {
    final List<T> list = toList();
    list.sort((a, b) {
      final R? valueA = selector(a);
      final R? valueB = selector(b);

      if (valueA == null && valueB == null) return 0;
      if (valueA == null) return -1;
      if (valueB == null) return 1;
      if (valueA is Comparable<R> && valueB is Comparable<R>) {
        return valueB.compareTo(valueA);
      } else if (valueA is num && valueB is num) {
        return valueB.compareTo(valueA);
      } else if (valueA is bool && valueB is bool) {
        return valueA ? 1 : -1;
      } else {
        throw ArgumentError('Selector must return a Comparable type');
      }
    });
    return list;
  }



  // 获取最大值
  T? max() {
    Iterator<T> iterator = this.iterator;
    if (!iterator.moveNext()) {
      // 如果迭代器不能移动到下一个元素，则返回null
      return null;
    }
    T max = iterator.current; // 获取当前元素作为初始最大值
    if (max is Comparable) {
      while (iterator.moveNext()) {
        // 移动到下一个元素并比较
        T e = iterator.current;
        if ((max as Comparable).compareTo(e) < 0) {
          max = e; // 更新最大值
        }
      }
    }
    return max;
  }

  // 获取最小值
  T? min() {
    Iterator<T> iterator = this.iterator;
    if (!iterator.moveNext()) {
      // 如果迭代器不能移动到下一个元素，则返回null
      return null;
    }
    T min = iterator.current; // 获取当前元素作为初始最大值
    if (min is Comparable) {
      while (iterator.moveNext()) {
        // 移动到下一个元素并比较
        T e = iterator.current;
        if ((min as Comparable).compareTo(e) > 0) {
          min = e; // 更新最小值
        }
      }
    }
    return min;
  }
}

extension IterableFlutureExtension<T> on Iterable<FutureFun<T>> {
  Future<List<T>> futureWait() {
    return Future.wait(map((f) => f()));
  }
}

void main() {
  [1, 2, 3, 4, 5.0].average();
  [1, 2, 3, 4, 5.0].map((it) => () async {}).futureWait();
}
